package game;

import java.util.Iterator;
import java.util.List;

import gameobj.Actor;
import gameobj.Background;
import gameobj.Boss;
import gameobj.Bullet;
import gameobj.ElementType;
import gameobj.Enemy;
import gameobj.GameObject;
import gameobj.Player;
import loader.Loader;
import manager.ElementManager;
import manager.ImageManager;
import manager.LevelManager;
import render.Model;
import render.SimpleModel;
import window.MainWindow;

public class GameThread extends Thread{
	private ElementManager em = null;
	private LevelManager lm = null;
	
	private boolean gameRunning = true;
	private boolean levelRunning = true;
	
	public GameThread() {
		em = ElementManager.get();
		lm = LevelManager.get();
	}
	
	@Override
	public void run() {
		while (gameRunning) {
			levelLoad();
			levelRun();
			levelEnd();
		}
	}
	
	public void levelLoad() {
		Loader.load("src/data/map" + lm.getLevel() + ".txt");
//		生成模型
		Model model = new SimpleModel("9.jpg-background");
		model.setWidth(MainWindow.WINDOWX);
		model.unifyScaleByWidth();
//		新建实体
		Background bg = new Background();
		Background bg1 = new Background();
//		注入模型
		bg.setModel(model);
		bg1.setModel(model);
		bg.setY(-780);
//		添加进管理器管理
		em.add(ElementType.ENVIRONMENT, bg);
		em.add(ElementType.ENVIRONMENT, bg1);

//		因为Player在切换关卡时不回收，故可通过此条件进行判定
		if (em.getList(ElementType.PLAYER).size() == 0) {
//			生成模型
			Model model2 = new SimpleModel("11.png-play");
//			新建实体
			Player player = new Player();
//			注入模型
			player.setModel(model2);
//			添加进管理器管理
			em.add(ElementType.PLAYER, player);
			player.setX(250);
			player.setY(600);

			player.setBulletModel(new SimpleModel("16.png-fire"));
		} else {
			Player player = (Player)em.getList(ElementType.PLAYER).get(0);
			player.setX(270);
			player.setY(600);
			player.setHealth(100);
			player.setMaxHealth(100);
			player.addattack = false;
		}
	}
	
	public void levelRun() {
		while (lm.isRunning()) {
			PK();
			for (ElementType type : ElementType.values()) {
//				用迭代器的方式进行循环可以使得删除操作得以进行
				Iterator<GameObject> it = em.getList(type).iterator();
				while (it.hasNext()) {
					GameObject obj = it.next();
//					如果是非存活的物体，则销毁
					if (!obj.isAlive()) {
						it.remove();
						continue;	// 然后直接进入下一个循环
					}
					if (obj.isActive()) {
						obj.ObjRun();
					}
				}
			}
			
//			每次主循环末尾睡眠一下
			try {
//				输出一下子弹池的数量
				//System.out.println("子弹List数量： " + em.getList(ElementType.ITEM).size());
				sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void levelEnd() {
		lm.levelUp();
		if (lm.getLevel() > 3) {
			gameRunning = false;
		}
		em.reset();
		
		try {
			sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lm.setRunning(true);
	}
	
	
// 碰撞检测
	public void PK ()
	{
		List<GameObject> players = em.getList(ElementType.PLAYER);
		List<GameObject> enemys  = em.getList(ElementType.ENEMY);
		List<GameObject> bosses   = em.getList(ElementType.BOSS);
		List<GameObject> items   = em.getList(ElementType.ITEM);
		for(int i=0;i<items.size();i++)
		{
			Bullet item = (Bullet)items.get(i);
			if( item.isPlayerOwned() )
			{
				for(int j=0;j<enemys.size();j++)
				{
					Actor enemy = (Actor)enemys.get(j);
					if( enemy.under_attack(item) )
					{
						System.out.println("<<enemy_under_attack>>");
						enemy.getHurt(item.getAttack());
						item.getHurt(0);
					}
				}
				//以下为boss受击
				for(int j=0;j<bosses.size();j++)
				{
					Boss boss =(Boss)bosses.get(j);
					if( boss.under_attack(item) )
					{
						System.out.println("<<boss_under_attack>>");
						boss.getHurt(item.getAttack());
						item.getHurt(0);
					}
				}
			}
			else
			{
				for(int j=0;j<players.size();j++)
				{
					Player player = (Player)players.get(j);
					if( player.under_attack(item) )
					{
						System.out.println("<<player_under_attack>>");
						player.getHurt(item.getAttack());
						item.getHurt(0);
					}
				}
			}
		}
	}
	
}
